package cn.iocoder.yudao.module.tms.service.customerfollowlog;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.tms.controller.admin.customerfollowlog.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.customerfollowlog.CustomerFollowLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.tms.dal.mysql.customerfollowlog.CustomerFollowLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.tms.enums.ErrorCodeConstants.*;

/**
 * 客户跟进日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CustomerFollowLogServiceImpl implements CustomerFollowLogService {

    @Resource
    private CustomerFollowLogMapper customerFollowLogMapper;

    @Override
    public Long createCustomerFollowLog(CustomerFollowLogSaveReqVO createReqVO) {
        // 插入
        CustomerFollowLogDO customerFollowLog = BeanUtils.toBean(createReqVO, CustomerFollowLogDO.class);
        customerFollowLogMapper.insert(customerFollowLog);

        // 返回
        return customerFollowLog.getId();
    }

    @Override
    public void updateCustomerFollowLog(CustomerFollowLogSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerFollowLogExists(updateReqVO.getId());
        
        // 更新
        CustomerFollowLogDO updateObj = BeanUtils.toBean(updateReqVO, CustomerFollowLogDO.class);
        customerFollowLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerFollowLog(Long id) {
        // 校验存在
        validateCustomerFollowLogExists(id);
        // 删除
        customerFollowLogMapper.deleteById(id);
    }

    private void validateCustomerFollowLogExists(Long id) {
        if (customerFollowLogMapper.selectById(id) == null) {
            throw exception(CUSTOMER_FOLLOW_LOG_NOT_EXISTS);
        }
    }

    @Override
    public CustomerFollowLogDO getCustomerFollowLog(Long id) {
        return customerFollowLogMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerFollowLogDO> getCustomerFollowLogPage(CustomerFollowLogPageReqVO pageReqVO) {
        return customerFollowLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomerFollowLogDO> getCustomerFollowLogListByFollowUpId(Long followUpId) {
        return customerFollowLogMapper.selectListByFollowUpId(followUpId);
    }

    @Override
    public List<CustomerFollowLogDO> getCustomerFollowLogListByCompanyId(Long companyId) {
        return customerFollowLogMapper.selectListByCompanyId(companyId);
    }

}
